$(function() {

    var music = Array(3);
    var $musicTabs = $('#music_tabs a');
    var $musicCont = $('.jqScrollSet');
    var currentMusic = null;

    function playMp3(url, id) {
        return jwplayer(id).setup({
            flashplayer: '/mediaplayer/jwplayer.flash.swf',
            file: url,
            skin: "/mediaplayer/skin/sp/six.xml",
            autostart: false,
            repeat: false,
            height: 40,
            width: 440
        });
    }

    function initMusic(idx) {
        var musicId = 'musicPlayer' + idx;
        var musicUrl = $musicTabs.eq(idx).data('href');
        var player = playMp3(musicUrl, musicId);
        music[idx] = player;
    }

    function stopAll() {
        for (var i = 0, l = music.length; i < l; i++) {
            if (music[i]) {
                music[i].stop();
            }
        }
    }

    $(".right .list .photo .inner .jCarouselLite").jCarouselLite({
        btnNext: ".right .list .photo .inner .next",
        btnPrev: ".right .list .photo .inner .prev"
    });

    $('.lightbox').lightBox({ overlayOpacity: 0.6 });

    //右边手风琴
    /*$('.right .list .inner:gt(0)').slideUp(0);
    $('.right .list .inner:eq(0)').slideDown();
    $('.right .list .tit:eq(0)').addClass('current');*/
    $('.right .list .inner').slideDown();
    $('.right .list .tit').addClass('current');

    $('.right .list .tit').click(function() {
        if ($(this).hasClass('current')) {
            $(this).next('.right .list .inner').slideUp();
            $(this).removeClass('current');
        } else {
            $(this).next('.right .list .inner').slideDown();
            $(this).addClass('current');
        }
        if ($(this).hasClass('isMusic')) {

            stopAll();

            if (!music[0]) {
                initMusic(0);
            }
            if ($musicCont.eq(0).data('panel') != 'true') {
                $musicCont.eq(0).find('.scroll-pane').jScrollPane().data('panel', 'true');
            }
        }
    });

    $musicTabs.on('click', function() {
        if (!$(this).hasClass('navActive')) {
            var i = $musicTabs.filter('.navActive').index();
            var idx = $(this).index();
            if (!music[idx]) {
                initMusic(idx);
            }
            if (music[i] !== 'undefined') {
                music[i].stop();
            }

            $musicTabs.removeClass('navActive');
            $(this).addClass('navActive');
            $musicCont.hide(0);
            $($musicCont[idx]).show();

            if ($musicCont.eq(idx).data('panel') != 'true') {
                $musicCont.eq(idx).find('.scroll-pane').jScrollPane().data('panel', 'true');
            }
        }
    });
});


$(function() {
    var $tvs = $('#tv-player .tv-list a');
    if ($tvs.length < 1) return;

    function getQueryString(name) {
        var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
        if (reg.test(location.href)) return decodeURI(RegExp.$2.replace(/\+/g, " "));
        return "";
    }

    var TV_Width = window.TV_Width || 700,
        TV_Height = window.TV_Height || 500,
        TV_HTML = window.TV_HTML || '<embed src="$SRC$" allowfullScreen="true" wmode="transparent" quality="high" width="$WIDTH$" height="$HEIGHT$" align="middle" allowScriptaccess="always" type="application/x-shockwave-flash"></embed>';

    TV_HTML = TV_HTML.replace(/\$WIDTH\$/g, TV_Width);
    TV_HTML = TV_HTML.replace(/\$HEIGHT\$/g, TV_Height);

    var $TV_Player = $('#tv-player .player'),
        $title = $('#tv-player h2 span'),
        idx = getQueryString('idx') || 1;

    $tvs.on('click', function() {
        if ($(this).hasClass('active')) return false;
        var href = this.href,
            title = $(this).text();
        if (!href) {
            alert('敬请期待！');
        } else {
            var _TV_HTML = TV_HTML.replace(/\$SRC\$/g, href);
            $TV_Player[0].innerHTML = _TV_HTML;
            $title.text(title);
        }
        $(this).addClass('active').siblings('.active').removeClass('active');
        return false;
    }).eq(idx - 1).trigger('click');
});